package com.sphd.emanu.view.dataround;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.sphd.emanu.R;

/**
 * 自定义控件之 数据圆环的展示
 *
 * @Author Hu
 * @Time 2017/5/15 --11:16.
 * @Email 87683202@qq.com
 */
public class DataRound extends View {

    private Paint paintRound;//绘制矩形画笔

    private Paint paintArc;//弧线画笔

    private float padding = DensityUtil.dip2px(getContext(), 6);

    private float arcRadian=0.0f;

    private int roundColor = 0x00000000;//圆的颜色

    private int arcColor = 0x0000ffff;//弧的颜色


    private float roundArcSize = DensityUtil.dip2px(getContext(), 6);//圆弧宽度


    private RectF arcRectF;//圆弧矩形

    private int mWidth;//宽度
    private int mHeight;//高度
    private Context context;
    private int dpForRxRy = DensityUtil.dip2px(getContext(), 10);


    public DataRound(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
        initPaint();
    }


    /*获取数据*/
    public void init(Context context, AttributeSet attrs) {
        this.context = context;

        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MainRoundTextView, 0, 0);

        roundColor = typedArray.getColor(R.styleable.MainRoundTextView_round_color, roundColor);
        arcColor = typedArray.getColor(R.styleable.MainRoundTextView_round_arc_color, arcColor);

        arcRadian=typedArray.getFloat(R.styleable.MainRoundTextView_radian, arcRadian);

        roundArcSize = typedArray.getDimension(R.styleable.MainRoundTextView_round_arc_size, roundArcSize);
        dpForRxRy = (int) typedArray.getDimension(R.styleable.MainRoundTextView_rectf_rx_ry, dpForRxRy);//矩形的圆角大小

        padding = typedArray.getDimension(R.styleable.MainRoundTextView_round_padding, padding);


    }

    private void initPaint() {

        paintRound = new Paint();
        paintArc = new Paint();

        paintRound.setAntiAlias(true);
        paintArc.setAntiAlias(true);

        paintRound.setStyle(Paint.Style.STROKE);
        paintArc.setStyle(Paint.Style.STROKE);

        paintRound.setColor(roundColor);
        paintArc.setColor(arcColor);

        arcRectF = new RectF();
    }

    /*EXACTLY：一般是设置了明确的值或者是MATCH_PARENT
   AT_MOST：表示子布局限制在一个最大值内，一般为WARP_CONTENT
   UNSPECIFIED：表示子布局想要多大就多大，很少使用*/
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        mWidth = widthSize;
        mHeight = widthSize;

        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        drawRound(canvas);
        drawArc(canvas);
        Log.e("margin", "margin" + padding + "\nheight" + mHeight
        );
    }


    //画出弧度
    private void drawArc(Canvas canvas) {

        int center = getHeight() / 2;//圆心
        int innerCircle = (int) ((getHeight() - padding * 2) / 2 - roundArcSize); //设置内圆半径
        //第一个参数是RectF   左上的x y坐标   右下的x y坐标
        //第二个参数是 弧形的开始角度
        //第三个参数是 弧形的结束角度
        //第四个参数是 true:画扇形   false:画弧线
        //第五个参数是 画笔
        //绘制圆环
        paintArc.setStrokeWidth(roundArcSize);
        arcRectF.set(padding + roundArcSize, padding + roundArcSize, center + innerCircle, center + innerCircle);
        canvas.drawArc(arcRectF, 270, arcRadian, false, paintArc);

    }

    private void drawRound(Canvas canvas) {
        int center = getHeight() / 2;//圆心
        int innerCircle = (int) ((getHeight() - padding * 2) / 2 - roundArcSize); //设置内圆半径
        //绘制圆环
        paintRound.setStrokeWidth(roundArcSize);
        canvas.drawCircle(center, center, innerCircle, paintRound);
    }

    public float getArcRadian() {
        return arcRadian;
    }

    public void setArcRadian(float arcRadian) {
        this.arcRadian = arcRadian;
        invalidate();

    }
}
